/*
 *-----------------------------------------------------------------------------
 *
 * TIndexerRpcDispatcher.java
 *
 * This file was created from IIndexerRpc.idl
 * by the Simple JavRMI backend.
 *
 * Copyright 2008-2010, Evgeny Alexeyev
 * Licensed under the EPL - v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 *
 *-----------------------------------------------------------------------------
 */

package com.googlecode.umlsynch.rpc;

import java.io.*;

import com.googlecode.umlsynch.rpc.IIndexerRpc.TRelationRpc;
import com.googlecode.umlsynch.rpc.base.TIOHandler;

import com.googlecode.umlsynch.rpc.base.IDebug;

import com.googlecode.umlsynch.rpc.base.TDebug;

import com.googlecode.umlsynch.rpc.base.IDispatcher;

import com.googlecode.umlsynch.rpc.TFileDescriptor;

import com.googlecode.umlsynch.rpc.TClassDescriptor;

import com.googlecode.umlsynch.rpc.IIndexerRpc;


public class TIndexerRpcDispatcher implements IDispatcher
{
  private IIndexerRpc implementation = null;
  private TDebug debug;
  
  public TIndexerRpcDispatcher(IDebug dbg, IDispatcher disp, IIndexerRpc impl) {
    debug = new TDebug(dbg);
    if (disp != null)
      disp.register(this);
    
    implementation = impl;
  }
  public boolean match(String interfaceName) {
    if (interfaceName.equals("IIndexerRpc"))
      return true;
    return false;
  }
  
  public void register(IDispatcher disp) {
    disp.register(this);
  }
  
  public void process(TIOHandler handler) {
    String data = handler.readString();
    
    debug.Note("Recieved methodname:" + data);
    
    
    if (data.equals( "GetClassRelations")) {
      GetClassRelations(handler);
      return;
    }
    
    if (data.equals( "GetClassInfo")) {
      GetClassInfo(handler);
      return;
    }
    
    debug.Error("Unknown method name:" + data);
  }
  
  private void GetClassRelations(TIOHandler handler) {
    String data = null;
    TClassDescriptor[] result = null;
    TClassDescriptor desc;
    TRelationRpc relationType;
    TFileDescriptor[] searchRestrictions;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassDescriptor[]")) {
        desc = (TClassDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor[]");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("TRelationRpc")) {
        relationType = (TRelationRpc) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TRelationRpc");
        throw new ClassNotFoundException();
      }
      
      data = handler.readString();
      if (data.equals("TFileDescriptor[]")) {
        searchRestrictions = (TFileDescriptor[]) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TFileDescriptor[]");
        throw new ClassNotFoundException();
      }
      
      result = implementation.GetClassRelations(desc, relationType, searchRestrictions);
      handler.getObjectOutputStream().writeObject(new String("TClassDescriptor[]"));
      handler.getObjectOutputStream().writeObject(result);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
  
  private void GetClassInfo(TIOHandler handler) {
    String data = null;
    TClassDescriptor result = null;
    TClassDescriptor desc;
    
    
    try {
      data = handler.readString();
      if (data.equals("TClassDescriptor")) {
        desc = (TClassDescriptor) handler.getObjectInputStream().readObject();
      } else {
        debug.Error("Recieved wrong parameter type:" + data + " expected TClassDescriptor");
        throw new ClassNotFoundException();
      }
      
      result = implementation.GetClassInfo(desc);
      handler.getObjectOutputStream().writeObject(new String("TClassDescriptor"));
      handler.getObjectOutputStream().writeObject(result);
    }
    catch (IOException e) {
    }
    catch (ClassNotFoundException e) {
    }
  }; // 
  
}; // IIndexerRpc

